package com.github.davidfantasy.mybatisplus.generatorui.util;

import com.baomidou.mybatisplus.generator.config.po.TableField;
import org.springframework.util.ObjectUtils;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

public class PropertyTypeSelf {

    // 确定时间类型
    public static boolean dateAnn(Object propertyType){
        return Optional.ofNullable(propertyType).map(type -> type.toString().toLowerCase().contains("date")).orElse(false);
    }

    public static String parseFieldAnn(List<TableField> fields){
        StringBuffer sb = new StringBuffer();
        if (fields.stream().filter(field -> field !=null && field.isKeyFlag()).findFirst().isPresent()) {
            sb.append("import com.join.datagateway.annotation.PK;").append(System.lineSeparator());
        }
        List<String> types = fields.stream().map(TableField::getPropertyType).filter(type->!ObjectUtils.isEmpty(type)).distinct().collect(Collectors.toList());
        if (types.stream().filter(type -> type.toString().toLowerCase().contains("date")).findFirst().isPresent()) {
            sb.append("import java.util.Date;").append(System.lineSeparator())
                    .append("import com.join.datagateway.annotation.DateField;").append(System.lineSeparator());
        }
        if (types.stream().filter(type -> type.toString().toLowerCase().contains("bigdecimal")).findFirst().isPresent()) {
            sb.append("import java.math.BigDecimal;").append(System.lineSeparator());
        }
        return sb.toString();
    }

    public static String parseObj(Object table){
        return "";
    }

}
